package com.atguigu.ssyx.user.service.Impl;

import com.atguigu.ssyx.model.user.Leader;
import com.atguigu.ssyx.model.user.User;
import com.atguigu.ssyx.model.user.UserDelivery;
import com.atguigu.ssyx.user.mapper.LeaderMapper;
import com.atguigu.ssyx.user.mapper.UserDeliveryMapper;
import com.atguigu.ssyx.user.mapper.UserMapper;
import com.atguigu.ssyx.user.service.UserService;
import com.atguigu.ssyx.vo.user.LeaderAddressVo;
import com.atguigu.ssyx.vo.user.UserLoginVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    private LeaderMapper leaderMapper;

    @Resource
    private UserDeliveryMapper userDeliveryMapper;

    /**
     * 根据openId查询用户信息
     * @param openId
     * @return
     */
    @Override
    public User getUserByOpenId (String openId) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getOpenId, openId);
        return baseMapper.selectOne(wrapper);
    }

    /**
     * 根据用户Id查询提货点和团长信息
     * @param userId
     * @return
     */
    @Override
    public LeaderAddressVo getLeaderAddressByUserId (Long userId) {
        // 根据userId查询用户默认的团长id
        LambdaQueryWrapper<UserDelivery> deliveryWrapper = new LambdaQueryWrapper<UserDelivery>();
        deliveryWrapper.eq(UserDelivery::getUserId, userId)
                .eq(UserDelivery::getIsDefault, 1);
        UserDelivery userDelivery = userDeliveryMapper.selectOne(deliveryWrapper);

        if (userDelivery == null) {
            return null;
        }
        // 根据团长id查询leader团长信息，如提货点、地址等
        Leader leader = leaderMapper.selectById(userDelivery.getLeaderId());
        // 封装数据
        return findLeaderAddressVo(userId, leader, userDelivery);
    }

    /**
     * 获取当前用户的登录信息
     * @param userId
     * @return
     */
    @Override
    public UserLoginVo getUserLoginVo (Long userId) {
        User user = baseMapper.selectById(userId);
        return findUserLoginVo(userId, user);
    }

    // 查询UserDelivery配送信息
    private UserDelivery findUserDelivery(Long userId) {
        LambdaQueryWrapper<UserDelivery> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserDelivery::getUserId, userId)
                .eq(UserDelivery::getIsDefault, 1);
        return userDeliveryMapper.selectOne(wrapper);
    }

    /**
     * 用于UserLoginVo数据处理
     */
    private UserLoginVo findUserLoginVo(Long userId, User user) {
        UserLoginVo userLoginVo = new UserLoginVo();
        userLoginVo.setUserId(userId);
        userLoginVo.setNickName(user.getNickName());
        userLoginVo.setPhotoUrl(user.getPhotoUrl());
        userLoginVo.setIsNew(user.getIsNew());
        userLoginVo.setOpenId(user.getOpenId());
        UserDelivery userDelivery = findUserDelivery(userId);
        if (userDelivery != null) {
            userLoginVo.setLeaderId(userDelivery.getLeaderId());
            userLoginVo.setWareId(userDelivery.getWareId());
        } else {
            userLoginVo.setLeaderId(1L);
            userLoginVo.setWareId(1L);
        }
        return userLoginVo;
    }

    /**
     * 用于LeaderAddressVo数据处理
     */
    private LeaderAddressVo findLeaderAddressVo(Long userId, Leader leader, UserDelivery userDelivery) {
        LeaderAddressVo leaderAddressVo = new LeaderAddressVo();
        BeanUtils.copyProperties(leader, leaderAddressVo);
        leaderAddressVo.setUserId(userId);
        leaderAddressVo.setLeaderId(leader.getId());
        leaderAddressVo.setLeaderName(leader.getName());
        leaderAddressVo.setLeaderPhone(leader.getPhone());
        leaderAddressVo.setWareId(userDelivery.getWareId());
        leaderAddressVo.setStorePath(leader.getStorePath());
        return leaderAddressVo;
    }
}
